package com.xjh.t.controller;


import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xjh.t.common.DataGridView;
import com.xjh.t.common.ResultObj;
import com.xjh.t.common.WebUtils;
import com.xjh.t.domain.ClientUser;
import com.xjh.t.domain.Goods;
import com.xjh.t.domain.UserCart;
import com.xjh.t.mapper.UserCartMapper;
import com.xjh.t.service.IClientUserService;
import com.xjh.t.service.IGoodsService;
import com.xjh.t.service.IUserCartService;
import com.xjh.t.vo.GoodsVo;
import com.xjh.t.vo.UserCartVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author satela
 * @since 2025-11-09
 */
@RestController
@RequestMapping("/user-cart")
public class UserCartController {

    @Autowired
    private IUserCartService userCartService;
    @Autowired
    private IGoodsService goodsService;


    @RequestMapping("addGoods")
    public Object addGoods(int goodsId,int count) {

        HttpSession session = WebUtils.getSession();
        ClientUser user =(ClientUser) session.getAttribute("user");

        QueryWrapper<UserCart> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", user.getId());
        queryWrapper.eq("goods_id", goodsId);

        List<UserCart> userCarts = userCartService.list(queryWrapper);
        for(UserCart userCart:userCarts)
        {
            if(userCart.getGoodsId() == goodsId)
            {
                userCart.setGoodsCount(userCart.getGoodsCount() + count);
                userCartService.updateById(userCart);

                JSONObject jsonObject = new JSONObject();
                JSONObject data = new JSONObject();

                jsonObject.put("code",200);
               // data.put("num",userCartService.getBaseMapper().selectCount(queryWrapperUser));
                data.put("cartId",userCart.getId());

                jsonObject.put("data",data);
                jsonObject.put("msg","加入购物成功");

                return jsonObject;

            }
        }


        Goods goods = goodsService.getById(goodsId);
        if(goods != null && goods.getSellOff() == 0)
        {
            UserCart userCart = new UserCart();
            userCart.setGoodsCount(count);
            userCart.setGoodsId(goodsId);
            userCart.setAddSalerPrice(goods.getSalerPrice());

            userCart.setAddPrice(goods.getSellPrice());
            userCart.setUserId(user.getId());
            userCart.setAddTime(new Date());
            userCartService.save(userCart);
            QueryWrapper<UserCart> queryWrapperUser = new QueryWrapper<>();
            queryWrapper.eq("user_id", user.getId());

           // List<UserCart> userAllCarts = userCartService.list(queryWrapperUser);
            JSONObject jsonObject = new JSONObject();
            JSONObject data = new JSONObject();

            jsonObject.put("code",200);
            data.put("num",userCartService.getBaseMapper().selectCount(queryWrapperUser));
            data.put("cartId",userCart.getId());

            jsonObject.put("data",data);
            jsonObject.put("msg","加入购物成功");

            return jsonObject;
        }
        else
        {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("code",0);
            jsonObject.put("msg","商品不存在或已下架");

            return jsonObject;
        }
    }

    @RequestMapping("getUserCartList")
    public DataGridView getUserCartList() {

        HttpSession session = WebUtils.getSession();
        ClientUser user =(ClientUser) session.getAttribute("user");


        QueryWrapper<UserCart> userCartQueryWrapper = new QueryWrapper<>();
        userCartQueryWrapper.eq("user_id",user.getId());

        List<UserCart> userCarts = userCartService.list(userCartQueryWrapper);

        List<UserCartVo> userCartVoList = new ArrayList<>();
        for(UserCart userCart:userCarts)
        {
            UserCartVo userCartVo = new UserCartVo();
            BeanUtils.copyProperties(userCart,userCartVo);

            Goods goods = goodsService.getById(userCart.getGoodsId());
            if(goods != null) {
                
                userCartVo.setGoods(goods);
                userCartVoList.add(userCartVo);

            }
        }
        return new DataGridView(userCarts.size(),userCartVoList);
    }


    @RequestMapping("getUserCartCount")
    public Object getUserCartCount() {

        HttpSession session = WebUtils.getSession();
        ClientUser user =(ClientUser) session.getAttribute("user");


        QueryWrapper<UserCart> userCartQueryWrapper = new QueryWrapper<>();
        userCartQueryWrapper.eq("user_id",user.getId());

        int count = userCartService.getBaseMapper().selectCount(userCartQueryWrapper);

        JSONObject jsonObject = new JSONObject();
        JSONObject data = new JSONObject();

        jsonObject.put("code",200);
        data.put("num",count);
        jsonObject.put("data",data);

        jsonObject.put("msg","查询购物车总数成功");
        return jsonObject;
    }

    @RequestMapping("updateGoodCount")
    public Object updateGoodCount(int cartId,int count) {

        if(count < 0)
        {
            return ResultObj.UPDATE_ERROR;
        }
        HttpSession session = WebUtils.getSession();
        ClientUser user =(ClientUser) session.getAttribute("user");



        UserCart userCart = userCartService.getById(cartId);

        if(userCart != null)
        {
            if(count > 0) {
                userCart.setGoodsCount(count);
                userCartService.updateById(userCart);

            }
            else
            {
                userCartService.removeById(userCart);
            }

            QueryWrapper<UserCart> userCartQueryWrapper = new QueryWrapper<>();
            userCartQueryWrapper.eq("user_id",user.getId());

            JSONObject jsonObject = new JSONObject();
            JSONObject data = new JSONObject();

            jsonObject.put("code",200);
            data.put("cartId",cartId);
            data.put("num",count);
            data.put("total",userCartService.getBaseMapper().selectCount(userCartQueryWrapper));
            jsonObject.put("data",data);

            jsonObject.put("msg","查询购物车总数成功");

            return jsonObject;
        }


        return ResultObj.UPDATE_ERROR;


    }
}

